package fnord.illuminati; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.Random; 
 
import fnord.exception.MapLoaderException; 
import fnord.illuminati.card.Card; 
 
public class Deck implements Serializable { 
    private static final long serialVersionUID = -2142713392416818551L; 
     
    private static String _cardlist = "./lib/cardlist.dat"; 
    private static String _cardList2 = "./cardlist.dat"; 
    private final Random _random = new Random(); 
     
    private ArrayList<Card> _cards; 
     
    public Deck() throws MapLoaderException { 
        _cards = new ArrayList<Card>(); 
         
        File cardlist; 
        BufferedReader cardlistreader; 
         
        try { 
            cardlist = new File(Deck._cardlist); 
            cardlistreader = new BufferedReader(new FileReader(cardlist)); 
             
        } catch (FileNotFoundException e) { 
            try { 
                cardlist = new File(Deck._cardList2); 
                cardlist.setReadOnly(); 
                cardlistreader = new BufferedReader(new FileReader(cardlist)); 
            } 
            catch (FileNotFoundException e2) { 
                throw new MapLoaderException("Could not find card list at \""+Deck._cardlist+"\"."); 
            } 
             
        } 
        try { 
            ClassLoader cl = ClassLoader.getSystemClassLoader(); 
 
            String cardname; 
            cardlist.setReadOnly(); 
            while ((cardname=cardlistreader.readLine())!=null) { 
                Class<?> c = cl.loadClass(cardname); 
                returnCardMiddle((Card)c.newInstance()); 
            } 
        } 
        catch (IOException e) { 
            throw new MapLoaderException("Error reading from card list."); 
        } catch (ClassNotFoundException e) { 
            throw new MapLoaderException("Could not find Card in card list."); 
        } catch (InstantiationException e) { 
            throw new MapLoaderException("Could not create Card."); 
        } catch (IllegalAccessException e) { 
            throw new MapLoaderException("Card constructor is not in proper scope."); 
        } 
    } 
     
    public Card drawCard() { 
        return _cards.remove(_cards.size()-1); 
    } 
     
    public boolean isEmpty() { 
        return _cards.isEmpty(); 
    } 
     
    public void returnCardTop(Card c) { 
        _cards.add(c); 
    } 
     
    public void returnCardBottom(Card c) { 
        _cards.add(0,c); 
    } 
     
    public void returnCardMiddle(Card c) { 
        _cards.add((int)(_random.nextDouble()*(_cards.size()-1)),c); 
    } 
     
    public void shuffleDeck() { 
        ArrayList<Card> newcards = new ArrayList<Card>(_cards.size()); 
        for (int i=_cards.size()-1;i>=0;i--) 
            newcards.add(_cards.remove(i)); 
        for (Card c:newcards) 
            returnCardMiddle(c); 
    } 
     
    public static void main(String args[]) { 
        try { 
            Deck deck = new Deck(); 
            System.out.println("Loaded the following group cards:"); 
            for (Card c:deck._cards) 
                System.out.println(c.getName()); 
            deck.shuffleDeck(); 
            System.out.println("\nShuffled:"); 
            for (Card c:deck._cards) 
                System.out.println(c.getName()); 
        } catch (MapLoaderException e) { 
            e.printStackTrace(); 
        } 
    } 
} 